import 'dart:async';
import 'dart:convert';
import 'package:meiyunapp/entity/mqtt_message_bean_entity.dart';
import 'package:qrscan/qrscan.dart' as scanner;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'package:get/get_utils/src/extensions/internacionalization.dart';
import 'package:meiyunapp/common/font_style.dart';
import 'package:meiyunapp/generated/assets.dart';
import 'package:meiyunapp/generated/locales.g.dart';
import 'package:meiyunapp/page/collection/mqtt_message_collection.dart';
import 'package:meiyunapp/router/routers_pages.dart';
import 'package:meiyunapp/service/mqtt_services.dart';
import 'package:meiyunapp/service/store_services.dart';
import 'package:meiyunapp/style/k_color.dart';
import 'package:meiyunapp/utils/env_variable.dart';
import 'package:meiyunapp/utils/flutter_logs_utils.dart';
import 'package:meiyunapp/widget/app_image_widget.dart';
import 'package:meiyunapp/widget/app_tab_widget.dart';
import 'package:meiyunapp/widget/decor/custom_header_clip_path.dart';
import 'package:meiyunapp/widget/decor/custom_header_clip_path.dart';
import 'package:meiyunapp/widget/loading.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MinePage extends StatefulWidget {
  const MinePage({Key? key}) : super(key: key);

  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage>
    with AutomaticKeepAliveClientMixin {
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  late StreamSubscription _streamSubscription;

  void _onRefresh() async {
    await Future.delayed(const Duration(milliseconds: 1000));
    _refreshController.refreshCompleted();
  }

  void _onLoading() async {
    await Future.delayed(const Duration(milliseconds: 1000));
    _refreshController.loadComplete();
  }

  @override
  void initState() {
    _streamSubscription = mStreamController.stream.listen((event) {
      print("event:$event");
    });
    super.initState();

    StoreService.to.listenKey("name", (value) {
      print("change name：$value");
    });
  }

  @override
  void dispose() {
    _streamSubscription.cancel();
    _refreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SmartRefresher(
      controller: _refreshController,
      enablePullDown: true,
      enablePullUp: true,
      onRefresh: _onRefresh,
      onLoading: _onLoading,
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          backgroundColor: KColor.Color0C5BE5,
          actions: const [
            Padding(
              padding: EdgeInsets.only(right: 11),
              child: Icon(
                Icons.settings,
                color: Colors.white,
              ),
            )
          ],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ClipPath(
              //clipper: CustomHeaderClipPath(),
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 15),
                color: KColor.Color0C5BE5,
                child: Column(
                  children: [
                    Row(
                      children: [
                        Container(
                          alignment: Alignment.center,
                          child: ClipOval(
                            child: AppImageWidget(
                              url:
                                  "http://file-cdn.zulingwang.cn/zlb-user/userhead/2021/06/15/1623723766828.png?x-oss-process=style/default",
                              width: 50,
                              height: 50,
                            ),
                          ),
                        ),
                        const SizedBox(
                          width: 15,
                        ),
                        Text(LocaleKeys.name.tr,
                            style: AppFontStyles.style_whit_16),
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 20,bottom: 15,left: 20,right: 20),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          RichText(
                            textAlign: TextAlign.center,
                            text: TextSpan(
                              children: [
                                TextSpan(
                                    text: LocaleKeys.zero.tr,
                                    style: AppFontStyles.style_whit_16),
                                TextSpan(
                                    text: LocaleKeys.guanzhu.tr,
                                    style: AppFontStyles.style_whit_14),
                              ],
                            ),
                          ),
                          RichText(
                            textAlign: TextAlign.center,
                            text: TextSpan(
                              children: [
                                TextSpan(
                                    text: LocaleKeys.zero.tr,
                                    style: AppFontStyles.style_whit_16),
                                TextSpan(
                                  text: LocaleKeys.zld.tr,
                                  style: AppFontStyles.style_whit_14,
                                ),
                              ],
                            ),
                          ),
                          RichText(
                            textAlign: TextAlign.center,
                            text: TextSpan(
                              children: [
                                TextSpan(
                                    text: LocaleKeys.zero.tr,
                                    style: AppFontStyles.style_whit_16),
                                TextSpan(
                                  text: LocaleKeys.sc.tr,
                                  style: AppFontStyles.style_whit_14,
                                ),
                              ],
                            ),
                          ),
                          RichText(
                            textAlign: TextAlign.center,
                            text: TextSpan(
                              children: [
                                TextSpan(
                                    text: LocaleKeys.zero.tr,
                                    style: AppFontStyles.style_whit_16),
                                const TextSpan(
                                    text: "查看历史",
                                    style: AppFontStyles.style_whit_14),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.symmetric(horizontal: 15),
                      color: Colors.yellow,
                      width: double.infinity,
                    ),
                    const SizedBox(
                      height: 15,
                    ),
                    Stack(
                      alignment: Alignment.center,
                      children: [
                        SvgPicture.asset(
                          Assets.svgBj2,
                          fit: BoxFit.fill,
                        ),
                        const Positioned(
                          left: 70,
                          child: Align(
                              child: Text(
                            "尊贵的会员 您可享受VIP8大权益",
                            style: AppFontStyles.style_815204_16,
                          )),
                        )
                      ],
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(
              height: 25,
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Padding(
                    padding: EdgeInsets.only(left: 12),
                    child: Text(
                      "材料管理",
                      style: AppFontStyles.style_black_15,
                    ),
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      TabWidget(
                        assetsPath: Assets.svgIcMyCl,
                        title: "材料",
                        voidCallback: () {
                          Get.toNamed(RouterPath.page_vip);
                        },
                      ),
                      TabWidget(
                        assetsPath: Assets.svgIcMyCk,
                        title: "仓库",
                        voidCallback: () {
                          Get.toNamed(RouterPath.page_404);
                        },
                      ),
                      TabWidget(
                        assetsPath: Assets.svgIcMyDp,
                        title: "材料",
                        voidCallback: () {
                          //LogsUtils.wew();
                          FlutterLogs.exportLogs(exportType: ExportType.ALL);
                        },
                      ),
                      TabWidget(
                        assetsPath: Assets.svgIcMySb,
                        title: "设备",
                        voidCallback: () {
                          Get.snackbar("", EnvVariables.APP_ENV,
                              snackPosition: SnackPosition.BOTTOM,
                              backgroundColor: Colors.black38,
                              colorText: Colors.white);
                        },
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 25,
                  ),
                  const Padding(
                    padding: EdgeInsets.only(left: 12),
                    child: Text(
                      "其他管理",
                      style: AppFontStyles.style_black_15,
                    ),
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      TabWidget(
                        assetsPath: Assets.svgIcMyZxz,
                        title: "猪信值",
                        voidCallback: () {
                          StoreService.to.put("name", "newName");
                        },
                      ),
                      TabWidget(
                        assetsPath: Assets.svgIcMyKh,
                        title: "举报",
                        voidCallback: () {
                          Get.toNamed(RouterPath.page_web_html, arguments: {
                            "url": "http://192.168.199.242:8080/vip",
                            "title": "举报"
                          });
                        },
                      ),
                      TabWidget(
                        assetsPath: Assets.svgIcMyKh,
                        title: "客服",
                        voidCallback: () async {
                          var json =
                              await rootBundle.loadString(Assets.jsonUser);
                          print("--->$json");
                        },
                      ),
                      TabWidget(assetsPath: Assets.svgIcMyRz, title: "认证"),
                    ],
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 12),
                    child: IconButton(
                      onPressed: () async {
                        String? topic = await scanner.scan();
                        print("--->topic=$topic");
                        if (topic == null) {
                          return;
                        }
                        Map<String, String> map = {};
                        map["messageType"] = "1";
                        String json = jsonEncode(map);
                        print("--json:$json");
                        MqttServices.to.sendMessage(topic, json);
                      },
                      icon: const Icon(FontAwesomeIcons.qq, size: 32),
                    ),
                  ),

                  Text(LocaleKeys.test.trArgs(["rty"])),

                  Text(LocaleKeys.test2.trParams({'name': "张三", 'age': "19"})),

                  Center(child: Text(LocaleKeys.app_name.trArgs(["1"])))
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
